package web.ling.por

import org.codehaus.groovy.grails.web.util.WebUtils
import org.springframework.web.servlet.support.RequestContextUtils as RCU

class ProviderService {

    def messageSource
    def grailsLinkGenerator

    def getNavigation(){
       def webUtils = WebUtils.retrieveGrailsWebRequest()
       return [
               (messageSource.getMessage("navigation.orders", null, RCU.getLocale(webUtils.currentRequest))) : grailsLinkGenerator.link(mapping: "listOpenOrders"),
               (messageSource.getMessage("navigation.othercat", null, 'Some Category', RCU.getLocale(webUtils.currentRequest))) : "#"
        ]
    }

    boolean checkIfCanApply(Provider p, ProofreadingOrder o) {
        if(o == null || p == null) {
            return false
        }

        boolean canApply = true
        canApply = p.languages.any {it.description == o.language.description} &&
                    p.fieldsOfExpertise.any { it.name == o.fieldOfExpertise.name} &&
                    o.orderStatus.description == 'open' && !o.applyingProviders.any {it.provider == p}

        return canApply
    }

    boolean checkIfCanRemove(Provider p, ProofreadingOrder o) {
        if(o == null || p == null) {
            return false
        }

        boolean canRemove = true
        canRemove = Application.findByOrderAndProvider(o,p) ? 'true':'false'

        return canRemove
    }

}
